package us.lithub.data;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * The Class BookParser.
 */
public class BookParser {
	

	private static final String TAG = BookParser.class.getSimpleName();
	
	/**
	 * Instantiates a new book parser.
	 */
	public BookParser(){
		
	}
	
	/**
	 * Gets the book attributes from isbn.
	 *
	 * @param isbn the isbn
	 * @return the book attributes from isbn
	 */
	public static Map<String, String> getBookAttributesFromIsbn(String isbn) {
		return getBookAttributesFromUpc(isbn);
	}

	/**
	 * Gets the book attributes from upc.
	 *
	 * @param upc the upc
	 * @return the book attributes from upc
	 */
	public static Map<String, String> getBookAttributesFromUpc(String upc) {

		try {
			final URL url = new URL("http://isbndb.com/api/books.xml?access_key=MIIRXTQX&index1=isbn&value1=" + upc);
			final SAXParserFactory spf = SAXParserFactory.newInstance();
			final SAXParser sp = spf.newSAXParser();	
			final XMLReader xr = sp.getXMLReader();	
			final IsbnDbHandler handler = new IsbnDbHandler();
			xr.setContentHandler(handler);
			xr.parse(new InputSource(url.openStream()));
			return handler.getAttributes();
		} catch (MalformedURLException e) {
			Log.e(TAG, e.getMessage());
		} catch (SAXException e) {
			Log.e(TAG, e.getMessage());
		} catch (ParserConfigurationException e) {
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}

		return null;
	}

	private static class IsbnDbHandler extends DefaultHandler {

		private Map<String, String> map;
		private boolean Title;
		private boolean TitleLong;
		private boolean AuthorsText;
		private boolean PublisherText;
		private boolean Summary;

		public IsbnDbHandler() {
			map = new HashMap<String, String>();
		}

		public Map<String, String> getAttributes() {
			return map;
		}

		@Override
		public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
			if (localName.equals("BookData")) {
				String isbn = atts.getValue(Book.ISBN);
				map.put(Book.ISBN, isbn);
			} else if (localName.equals(Book.TITLE)) {
				Title = true;
			} else if (localName.equals(Book.TITLE_LONG)) {
				TitleLong = true;
			} else if (localName.equals(Book.AUTHOR_TEXT)) {
				AuthorsText = true;
			} else if (localName.equals(Book.PUBLISHER_TEXT)) {
				PublisherText = true;
			} else if (localName.equals(Book.SUMMARY)) {
				Summary = true;
			}
		}

		@Override
		public void characters(char ch[], int start, int length) throws SAXException {
			if (Title) {
				map.put(Book.TITLE, new String(ch, start, length));
				Title = false;
			} else if (TitleLong) {
				map.put(Book.TITLE_LONG, new String(ch, start, length));
				TitleLong = false;
			} else if (AuthorsText) {
				map.put(Book.AUTHOR_TEXT, new String(ch, start, length).replaceAll(",", ""));
				AuthorsText = false;
			} else if (PublisherText) {
				map.put(Book.PUBLISHER_TEXT, new String(ch, start, length));
				PublisherText = false;
			} else if (Summary) {
				map.put(Book.SUMMARY, new String (ch, start, length));
				Summary = false;
			}
		}
	}
}
